// pages/index/index.js
const App = getApp()

Page({
  data: {
    isLogin: false,
    showModal: false,
    bannerList: [],
    isSigned:false,
    cashTotal:0,//积分总数
    couponTotal:0,//优惠券总数
    cardData: {
      avatarUrl: "../../images/logo.png",
      nickName: "银河便利店",
      number: "YH123456"
    },
    advantageList: [
      {icon: "../../images/icon_03.png", name: "实体会员卡"},
      {icon: "../../images/icon_04.png", name: "月度优惠券"},
      {icon: "../../images/icon_05.png", name: "积分兑好礼"},
      {icon: "../../images/icon_06.png", name: "月首单双倍积分"},
      {icon: "../../images/icon_07.png", name: "生日专属券"},
      {icon: "../../images/icon_08.png", name: "等级倍数积分"},
      {icon: "../../images/icon_09.png", name: "新品免费试"},
      {icon: "../../images/icon_10.png", name: "敬请期待"}
    ],
    freeProductList: [
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"}
    ],
    hotProductList: [
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"},
      {image: "../../images/banner.png", title: "白日梦系列PORO手办", presentPrice: "0", originalPrice: "599"}
    ],
    hasUserInfo: false,
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgInfoArrLength: 0,  // 轮播图列表的长度
    centerItem: 0,  // 居中项的位置
    cardList: [],
    myCard: {},
  },
  onShow() {
    this.setData({
      isLogin: App.checkIsLogin()
    });
    if (this.data.isLogin) {
      // 获取当前用户信息
      this.GetOrderTotal()
      this.getUserDetail();
    }
  },
  onLoad(){
    this.getBanner() //轮播图
    this.getNotice() //公告设置
    this.getCardList()
    this.getProductList() //获取产品
    // this.getProductClass() // 获取产品分类
  },
  //获取卡片
  getCardList(){
    this.setData({
      cardList:[{},{},{}]
    })
  },
  //获取用户详情
  getUserDetail(){
    let userInfo= wx.getStorageSync('userInfo')
    let session_Key = wx.getStorageSync('session_Key')
    this.setData({
      userInfo,
      session_Key
    })
  },
  // 获取用户信息
  getUserInfo(e) {
    let _this = this;
    App.getUserInfo(e, (res) => {
      wx.setStorageSync('userInfo', e.detail.userInfo);
      this.getUserDetail();
      if(res.token=='') {
        _this.setData({
          showModal: true,
          session_Key: res.session_Key
        })
      }else{
        this.GetOrderTotal()
        _this.setData({
          userInfo: e.detail.userInfo,
          isLogin: true,
        })
      }
    });
  },
  // 点击按钮获取手机号权限并解析
  getPhoneNumber(e) {
    const _this = this;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '友情提示',
        showCancel: true,
        content: '拒绝将无法获取微信手机号，需要您手动填写手机号注册手机号。',
        success: function (res) {
          if(res.confirm){
            _this.pageForward();
          }
        }
      })
    } else {
      const {encryptedData, iv} = e.detail;
      const options = {
        encryptedData: encryptedData,
        iv: iv,
        sessionKey: _this.data.session_Key
      };
      _this.doGetPhone(options);
    }
  },
  doGetPhone (options) {
    const _this = this;
    // 向服务器请求解密
    App._post_form('User/GetPhone', options, (res) => {
      // 最终获取到用户数据，国家代号前缀、不带前缀的手机号。默认是不带前缀
      const {countryCode, phoneNumber} = res;
      _this.pageForward(countryCode, phoneNumber);
      App.showSuccess(res.msg, () => {
        _this.setData(res)
      })
    }, (err) => {
      _this.pageForward();
    })
  },
  pageForward (countryCode, phoneNumber) {
    // 获取成功后我是跳转到另一个页面
    wx.navigateTo({
      url: `/pages/info/index?phone=${phoneNumber}`
    })
  },
  // swiper 切换方法
  changeFun(e) {
    this.setData({
      centerItem: e.detail.current,
    })
  },
  // 获取轮播图
  getBanner(){
    App._get('Site/Api008',{},(res)=>{
      let bannerList = res.data
      this.setData({bannerList})
    })
  },
  //获取公告
  getNotice(){
    App._get('Site/Api004',{},(res)=>{
      this.setData({
        noticeList:res.items
      })
    })
  },
  //获取商品
  getProductList(){
    App._get('Product/Api002',{},(data)=>{
      console.log(data)
      this.setData({
        productList:data.items
      })
    })
  },
  //获取商品分类
  getProductClass(){
    App._get('Product/Api001',{},(data)=>{
    })
  },
  //获取积分总数
  GetOrderTotal(){
    App._get('User/Api009',{},(res)=>{
        this.setData({
          cashTotal:res.data.cashTotal,
          couponTotal:res.data.coupon
        })
    })
  },
  // 获取商品列表
  getGoods(){
    App._get('Product/Api002',{a:1},(res)=>{

    })
  },
  closeModal(){
    this.setData({
      showModal:false
    })
  },
  // 每日签到
  signInHandle(){
    if(!this.data.isSigned){
      App._post_form('User/Api008',{},(data)=>{
        this.setData({
          isSigned:true
        })
        App.showMsg(data.msg)
      })
    }else{
      App.showMsg('您已经签过到了，明天再来吧。')
    }
  },
   // packages/goods/detail/index?alias=2ogsvq7s8mdue&shopAutoEnter=1
  // 跳转有赞
  toYouZan(){
    wx.navigateToMiniProgram({
      appId: 'wxaf5c846d2553b4f2',  //appid
      path: 'packages/goods/detail/index?alias=2ogsvq7s8mdue&shopAutoEnter=1',//path
      extraData: {  //参数
        alias:'37108615'
      },
      envVersion: 'release', //开发版develop 开发版 trial   体验版 release 正式版 
      success(res) {
        console.log('成功')
        // 打开成功
      }
    })
  }
})